package com.demo.money.controller.admin;

import com.demo.money.kit.JSONResult;
import com.demo.money.kit.Md5Until;
import com.demo.money.kit.example.CostdetailsExample;
import com.demo.money.kit.example.UserExample;
import com.demo.money.model.User;
import com.demo.money.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Index类<br>
 * index/login.html页面里的请求映射关系
 * <p>
 * <b>常用方法</b>：<br>
 * 1.landingVerification - 登录-验证
 * 2.loginGoToIndex - 登陆成功-重定向到index.html页面
 * <p>
 * <b>全类名</b>:com.demo.money.controller<br>
 * <b>类名</b>:Index<br>
 * <b>创建者</b>:张峰浦<br>
 * <b>创建时间</b>:2020/3/15 13:37<br>
 */
@RestController

public class AdminJson {
    @Autowired
    private UserService userService;


    /**
     * 登陆成功-重定向到index.html页面
     * <p>
     * 解决浏览器表单信息问题
     *
     * @return
     */
    @RequestMapping(value = {"adminuserlist", "adminuserlist.html"})
    public JSONResult loginGoToIndex(Model model) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andJurisdictionEqualTo("B");
        List<User> users = userService.selectByExample(userExample);
        model.addAttribute("lists",users);
        System.out.println(users);
        return JSONResult.ok(users);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = {"adminAddUser", "adminAddUser.html"})
    public JSONResult adminAddUser(User user) {

        String Md5Pass = Md5Until.setMd5(user.getPassword());

        user.setPassword(Md5Pass);

        int key= userService.insert(user);

        if (key == 1){

            return JSONResult.ok("用户添加成功！");

        }else{

            return JSONResult.errorMsg("用户添加失败！");

        }

    }

    /**
     * 账号查重
     *
     * @param user
     * @return
     */
    @RequestMapping(value = {"adminUserNameLoginEqualTo", "adminUserNameLoginEqualTo.html"})
    public Object adminUserNameLoginEqualTo(User user) {

        UserExample userExample = new UserExample();

        UserExample.Criteria criteria = userExample.createCriteria();

        criteria.andNameLoginEqualTo(user.getNameLogin());

        return userService.selectByExample(userExample).size();
    }

}
